from django.db import models
from apps.common.base.models import BaseModel
from apps.merchant.activity.models import Activity
from apps.user.models import UserModel
from apps.merchant.shop.models import Shop

class Order(BaseModel):

    orderStatus = (
        ("WAIT_PAY", "待支付"),
        ("WAIT_SEND", "待发货"),
        ("READY_SEND", "准备发货"),
        ("WAIT_RECEIVE", "待收货"),
        ("FINISH", "已完成"),
        ("CANCEL", "已取消"),
        ("RETURNING", "退款中"),
        ("HAS_RETURN", "已退款"),
        ("RETURN_FAIL", "退款失败")
    )
    returnStatus = (
        ("INITIAL", "初始状态"),
        ("RETURNING", "退款中"),
        ("REFUSE", "已拒绝"),
        ("FINISH", "已退款")
    )
    payway = (
        ('wxpay', "微信支付"),
        ('alipay', "支付宝支付"),
        ('incomeAndIncomeLocked', "资产组合支付"),
        ('incomes', "积分支付"),
        ('incomesLocked', "专项积分支付"),
        ('balance', "余额支付"),
    )
    sendType = (
        ('expressage', "快递"),
        ('selfPickup', "自提"),
        ('noSend', "无需发货")
    )
    scene = (
        ('generay', "普通订单"),
        ('recharge', "充值"),
        ('giftSection', "礼包专区"),
        ('payForFranchisee', "加盟商收款"),
    )
    order = models.ForeignKey('self', related_name="order_order", verbose_name='主订单', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    shop = models.ForeignKey(Shop, related_name="shop_order", verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    user = models.ForeignKey(UserModel, related_name='user_order', verbose_name='用户', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    rider = models.ForeignKey(UserModel, related_name='rider_order', verbose_name='骑手', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    activity = models.ForeignKey(Activity, related_name='activity_order', verbose_name='活动', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)

    content = models.CharField("订单内容", max_length=10000, default='')
    activityParams = models.CharField("活动参数", max_length=1000, default='')
    addrinfo = models.CharField("地址信息", max_length=500, blank=True, null=True, default='')
    expressageinfo = models.CharField("快递公司信息", max_length=500, blank=True, null=True, default='') # expressageCompany  expressageNum
    scene = models.CharField("订单场景", max_length=100, choices=scene, default='generay')
    mode = models.CharField("模式", max_length=100, default='')
    clientname=models.CharField("clientname", max_length=200, null=True, blank=True)
    orderId = models.CharField('订单ID', max_length=100, blank=True)
    transactionId = models.CharField('微信支付订单号', max_length=100, blank=True)
    orderStatus = models.CharField("订单状态", max_length=100, choices=orderStatus, default="WAIT_PAY")
    returnStatus = models.CharField("退款状态", max_length=100, choices=returnStatus, default="INITIAL")
    sellingPrice=models.DecimalField("订单销售价",max_digits=10, decimal_places=2, default=0)
    realPay=models.DecimalField("实际支付金额",max_digits=10, decimal_places=2, default=0)
    realPayIncomes = models.DecimalField("实际支付积分金额", max_digits=10, decimal_places=2, default=0)
    payway=models.CharField("付款类型", choices=payway, max_length=100, blank=True, null=True)
    payTime = models.DateTimeField('支付时间', blank=True, null=True)
    completeTime = models.DateTimeField('完成时间', blank=True, null=True)
    retunMoneyTime = models.DateTimeField('退款时间', blank=True, null=True)
    sendType = models.CharField("发货类型", choices=sendType, max_length=100, blank=True, null=True)
    remark = models.CharField('买家备注', max_length=100, blank=True, default='')
    remark_seller = models.CharField('卖家备注', max_length=100, blank=True, default='')

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = verbose_name